/*
  Copyright 2007 Anders Båtstrand

      Licensed under the Apache License, Version 2.0 (the "License");
      you may not use this file except in compliance with the License.
      You may obtain a copy of the License at

          http://www.apache.org/licenses/LICENSE-2.0

      Unless required by applicable law or agreed to in writing, software
      distributed under the License is distributed on an "AS IS" BASIS,
      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      See the License for the specific language governing permissions and
      limitations under the License.

 */
package org.dyndns.anderlin.jmplayer.client.domain;

import org.dyndns.anderlin.jmplayer.model.Artist;
import org.springframework.rules.Rules;
import org.springframework.rules.constraint.Constraint;
import org.springframework.rules.support.DefaultRulesSource;

import java.util.Date;

/**
 * Rules used for validating input
 */
public class JmplayerValidationRulesSource extends DefaultRulesSource {


    /**
     * Basic name validator. Note that the "alphabeticConstraint" argument is a message
     * key used to locate the message to display when this validator fails.
     */
    private final Constraint NAME_CONSTRAINT = all(new Constraint[]{required(), minLength(2),
            regexp("[-'.a-zA-ZæøåÆØÅ ]*", "alphabeticConstraint")});

    public JmplayerValidationRulesSource() {
        super();
        addRules(createArtistRules());
    }

    private Rules createArtistRules() {
        return new Rules(Artist.class) {
            protected void initRules() {
                add("firstname", NAME_CONSTRAINT);
                add("surname", NAME_CONSTRAINT);
                add("birth", required());
                add("birth", lt(new Date()));
                add("death", lte(new Date()));
            }
        };
    }
}
